package cn.cnsuh.dao.impl;


import cn.cnsuh.dao.GameDao;
import cn.cnsuh.domain.Game;
import cn.cnsuh.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Magic
 * @create 2020-05-19 0:17
 */
public class GameDaoImpl implements GameDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int findTotalCount(String gname) {

        String sql = " select count(*) from tab_game where 1 = 1 ";

        StringBuilder sb = new StringBuilder(sql);

        List params = new ArrayList();//参数条件们

        //判断参数是否有值
        if(gname != null &&gname.length()>0){
            sb.append(" and gname like ?  ");

            params.add("%"+gname+"%");
        }

        sql = sb.toString();


        return template.queryForObject(sql,Integer.class,params.toArray());
    }

    @Override
    public List<Game> findByPage(int start, int pageSize, String gname) {
        String sql = " select * from tab_game where 1=1 ";
        StringBuilder sb = new StringBuilder(sql);

        List params = new ArrayList();//条件们

        if(gname != null &&gname.length()>0){
            sb.append(" and gname like ?  ");

            params.add("%"+gname+"%");
        }

        sb.append(" limit ?,?  ");//分页条件
        sql = sb.toString();
        params.add(start);
        params.add(pageSize);


        return template.query(sql,new BeanPropertyRowMapper<Game>(Game.class),params.toArray());
    }
}
